from openai import OpenAI
import dotenv
import os

dotenv.load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"), base_url=os.getenv("BASE_URL"))
# 调用聊天补全API
response = client.chat.completions.create(
    # model="gpt-4o",
    model=os.getenv("MODEL_NAME"),
    messages=[
        {"role": "user", "content": "简单介绍一下“及至始皇，非六世之余烈”中是哪六世"}
    ],
    max_tokens=500,  # 最大生成的token数，用来控制回复长度
    temperature=0.7,  # 温度控制，控制回答的随机性(0-2,随大越随机)
    stream=True,  # 是否使用流式输出
)
# reply = response.choices[0].message.content
# print(reply)

# 处理流式输出
for chunk in response:
    print(chunk.choices[0].delta.content, end="", flush=True)
